package view;

import java.util.Date;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Dimension;

import javax.swing.border.LineBorder;

import controller.ACSystem;

import model.Auction;
import model.AuctionCalendar;
import model.AuctionDay;
import model.WestPanelEnum;

import com.toedter.calendar.JCalendar;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 * 
 * @author Bob Landowski
 * @author Kevin Smith
 *
 */

/**
 * The calendar pane class.
 */
@SuppressWarnings("serial")
public class CalendarPane extends JPanel {

	/** The ACcontroller */
	private ACSystem my_ACcontroller;

	/** The auction 1 */
	private Auction my_auction1;

	/** The auction 2 */
	private Auction my_auction2;

	/** The calendar */
	private JCalendar calendar;

	/** The auction 1 button */
	private final JButton auction1Button;

	/** The auction 2 button */
	private final JButton auction2Button;

	/**
	 * Create the panel.
	 * 
	 * @param my_ACcontroller2
	 */
	public CalendarPane(ACSystem my_ACcontroller2) {
		setBorder(new LineBorder(new Color(0, 0, 0), 5));
		setLayout(null);
		setPreferredSize(new Dimension(560, 560));
		my_ACcontroller = my_ACcontroller2;
		calendar = new JCalendar();
		calendar.setBounds(5, 5, 550, 490);

		calendar.addPropertyChangeListener(new CalendarChangeListener());

		add(calendar);

		auction1Button = new JButton("Auction 1");
		auction1Button.setEnabled(false); // disabled by default
		auction1Button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				auction1ActionPerformed(e);
			}
		});
		auction1Button.setBounds(37, 511, 115, 29);
		add(auction1Button);

		auction2Button = new JButton("Auction 2");
		auction2Button.setEnabled(false); // disabled by default
		auction2Button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				auction2ActionPerformed(e);
			}
		});
		auction2Button.setBounds(364, 511, 115, 29);
		add(auction2Button);

	}

	/** The auction 1 Action performed */
	private void auction1ActionPerformed(java.awt.event.ActionEvent e) {
		my_ACcontroller.setWestPanel(WestPanelEnum.AUCTION);
		my_ACcontroller.getMainFrame().westPanelState(my_auction1);

	}

	/** The auction 2 action performed */
	private void auction2ActionPerformed(java.awt.event.ActionEvent e) {
		my_ACcontroller.setWestPanel(WestPanelEnum.AUCTION);
		my_ACcontroller.getMainFrame().westPanelState(my_auction2);

	}

	/** The getObjectData method */
	public void getObjectData(Object the_object) {
	}

	/** The Calendar Chage Listener class */
	private class CalendarChangeListener implements PropertyChangeListener {

		@Override
		public void propertyChange(PropertyChangeEvent e) {
			Date temp = calendar.getDate();
			AuctionDay day = AuctionCalendar.getAuctions(temp);
			if (!day.is_empty) {
				if (day.getAuction1() != null) {
					auction1Button.setEnabled(true);
					my_auction1 = day.getAuction1();
				} else {
					auction1Button.setEnabled(false);
				}
				if (day.getAuction2() != null) {
					auction2Button.setEnabled(true);
					my_auction2 = day.getAuction2();
				} else {
					auction2Button.setEnabled(false);
				}
			} else {
				auction1Button.setEnabled(false);
				auction2Button.setEnabled(false);
			}
		}

	}
}
